package game.wormgame.mrworm;

import java.util.List;

import android.graphics.Color;
import android.util.Log;

import game.wormgame.framework.Game;
import game.wormgame.framework.Graphics;
import game.wormgame.framework.Input.TouchEvent;
import game.wormgame.framework.Input.KeyEvent;
import game.wormgame.framework.Pixmap;
import game.wormgame.framework.Screen;

public class GameScreen extends Screen {
    enum GameState {
        Ready,
        Running,
        Paused,
        GameOver
    }
    
    GameState state = GameState.Ready;
    World world;
    int oldScore = 0;
    String score = "0";
    
    private int font_width = 75;
    private int spacing_after_period = 75;
    
    public GameScreen(Game game) {
        super(game);
        world = new World();
    }

    @Override
    public void update(float deltaTime) {
        List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
        List<KeyEvent> keyEvents  = game.getInput().getKeyEvents();
        
        if(state == GameState.Ready)
            updateReady(touchEvents, keyEvents);
        if(state == GameState.Running)
            updateRunning(touchEvents, keyEvents, deltaTime);
        if(state == GameState.Paused)
            updatePaused(touchEvents);
        if(state == GameState.GameOver)
            updateGameOver(touchEvents);        
    }
    
    private void updateReady(List<TouchEvent> touchEvents, List<KeyEvent> keyEvents) {
        if ((touchEvents.size() > 0 ) || (keyEvents.size())> 0)
            state = GameState.Running;
    }
    
    private void updateRunning(List<TouchEvent> touchEvents, List<KeyEvent> keyEvents, float deltaTime) {        
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if(event.type == TouchEvent.TOUCH_UP) {
                if(event.x < 64 && event.y < 64) {
                    if(Settings.soundEnabled)
                        Assets.click.play(1);
                    state = GameState.Paused;
                    return;
                }
            }
            if(event.type == TouchEvent.TOUCH_DOWN) {
                if(event.x < 100 && event.y > 442) {
                    world.snake.turnLeft();
                }
                if(event.x > 1024 - 100 && event.y > 452) {
                    world.snake.turnRight();
                }
            }
        }
        
        int keylength = keyEvents.size();
        
        
        for (int i = 0; i < keylength; i++)
        {
        	KeyEvent keyEvent = keyEvents.get(i);
        	Log.i("GameScreen","I see Key: " + keyEvent.keyCode);
        	
        	
        	
        	if (keyEvent.keyCode == 22 )
        	{
        		world.snake.turnRight();
        		break;
        	}
        	if (keyEvent.keyCode == 21 )
        	{
        		world.snake.turnLeft();
        		break;
        	}
        	
        }
        
        world.update(deltaTime);
        if(world.gameOver) {
            if(Settings.soundEnabled)
                Assets.bitten.play(1);
            state = GameState.GameOver;
        }
        if(oldScore != world.score) {
            oldScore = world.score;
            score = "" + oldScore;
            if(Settings.soundEnabled)
                Assets.eat.play(1);
        }
    }
    
    private void updatePaused(List<TouchEvent> touchEvents) {
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if(event.type == TouchEvent.TOUCH_UP) {
                if(event.x > 362 && event.x <= 362 + 273) {
                    if(event.y > 552 - 167 - 92 && event.y < 552 - 167) {
                        if(Settings.soundEnabled)
                            Assets.click.play(1);
                        state = GameState.Running;
                        return;
                    }                    
                    if(event.y > 552 - 300 - 91 && event.y <= 552 - 300) {
                        if(Settings.soundEnabled)
                            Assets.click.play(1);
                        game.setScreen(new MainMenuScreen(game));                        
                        return;
                    }
                }
            }
        }
    }
    
    private void updateGameOver(List<TouchEvent> touchEvents) {
        int len = touchEvents.size();
        for(int i = 0; i < len; i++) {
            TouchEvent event = touchEvents.get(i);
            if(event.type == TouchEvent.TOUCH_UP) {
                if(event.x >= 460 && event.x <= 560 &&
                   event.y >= 552 - 157 - 100  && event.y <= 552 - 157 ) {
                    if(Settings.soundEnabled)
                        Assets.click.play(1);
                    game.setScreen(new MainMenuScreen(game));
                    return;
                }
            }
        }
    }
    

    @Override
    public void present(float deltaTime) {
        Graphics g = game.getGraphics();
        
        g.drawPixmap(Assets.background, 0, 0);
        drawWorld(world);
        if(state == GameState.Ready) 
            drawReadyUI();
        if(state == GameState.Running)
        {
            drawRunningUI();
            drawText(g, score, g.getWidth() / 2 - score.length()*font_width / 2, g.getHeight() - 100);
        }
        if(state == GameState.Paused)
            drawPausedUI();
        if(state == GameState.GameOver)
            drawGameOverUI();
                        
    }
    
    private void drawWorld(World world) {
        Graphics g = game.getGraphics();
        Snake snake = world.snake;
        SnakePart head = snake.parts.get(0);
        Stain stain = world.stain;
        
        
        Pixmap stainPixmap = null;
        if(stain.type == Stain.TYPE_1)
            stainPixmap = Assets.yarn;
        if(stain.type == Stain.TYPE_2)
            stainPixmap = Assets.bowl;
        if(stain.type == Stain.TYPE_3)
            stainPixmap = Assets.mouse1;
        int x = stain.x * 64 + 32;
        int y = stain.y * 64 + 32;      
        g.drawPixmap(stainPixmap,  x - stainPixmap.getWidth() /  2 , y - stainPixmap.getHeight() / 2);
       
        int len = snake.parts.size();
        for(int i = 1; i < len; i++) {
            SnakePart part = snake.parts.get(i);
            x = part.x * 64 + 32;
            y = part.y * 64 + 32;
            g.drawPixmap(Assets.tail,  x - Assets.tail.getWidth() / 2, y - Assets.tail.getHeight() / 2);
        }
        
        Pixmap headPixmap = null;
        if(snake.direction == Snake.UP) 
            headPixmap = Assets.headUp;
        if(snake.direction == Snake.LEFT) 
            headPixmap = Assets.headLeft;
        if(snake.direction == Snake.DOWN) 
            headPixmap = Assets.headDown;
        if(snake.direction == Snake.RIGHT) 
            headPixmap = Assets.headRight;        
        x = head.x * 64 + 32;
        y = head.y * 64 + 32;
        g.drawPixmap(headPixmap, x - headPixmap.getWidth() / 2, y - headPixmap.getHeight() / 2);
    }
    
    private void drawReadyUI() {
        Graphics g = game.getGraphics();
        
        g.drawPixmap(Assets.ready, 376, 552 - 429 - 72);
        g.drawPixmap(Assets.clicktocontinue, 273, 552 - 35 - 170);

    }
    
    private void drawRunningUI() {
        Graphics g = game.getGraphics();

        g.drawPixmap(Assets.buttons, 0, 0, 100, 200, 100, 100);
        g.drawLine(0, 416, 1024, 416, Color.BLACK);
        g.drawPixmap(Assets.buttons, 0, 442 , 100, 100, 100, 100);
        g.drawPixmap(Assets.buttons, 1024 - 100, 452, 0, 100, 100, 100);
    }
    
    private void drawPausedUI() {
        Graphics g = game.getGraphics();
        
        g.drawPixmap(Assets.pause, 362, 552 - 162 - 228);

    }

    private void drawGameOverUI() {
        Graphics g = game.getGraphics();
        
        g.drawPixmap(Assets.gameOver, 217, 552 - 284 - 117);
        g.drawPixmap(Assets.buttons, 460, 552 - 157- 100, 0, 200, 100, 100);
        g.drawLine(0, 416, 1024, 416, Color.BLACK);
        drawText(g, score, g.getWidth() / 2 - score.length()*font_width / 2, g.getHeight() - 100);
    }
    
    public void drawText(Graphics g, String line, int x, int y) {
        int len = line.length();
        for (int i = 0; i < len; i++) {
            char character = line.charAt(i);
    
            if (character == ' ') {
                x += spacing_after_period;
                continue;
            }
    
            int srcX = 0;
            int srcWidth = 0;
            if (character == '.') {
                srcX = 750;
                srcWidth = 37;
            } else {
                srcX = (character - '0') * font_width;
                srcWidth = font_width;
            }
    
            g.drawPixmap(Assets.numbers, x, y, srcX, 0, srcWidth, font_width);
            x += srcWidth;
        }
    }
    
    @Override
    public void pause() {
        if(state == GameState.Running)
            state = GameState.Paused;
        
        if(world.gameOver) {
            Settings.addScore(world.score);
            Settings.save(game.getFileIO());
        }
    }

    @Override
    public void resume() {
        
    }

    @Override
    public void dispose() {
        
    }
}